<template>
  <div class="login-main">
    <h4 class="login-title">
      {{ $t('login.authtitle') }}
    </h4>
    <el-form label-position="top" label-width="80px" :model="form" ref="form" size="small" class="authForm" :rules="dataRule">
      <el-form-item label="公司/企业名称" prop="companyName">
        <el-input v-model="form.companyName" spellcheck="false"></el-input>
      </el-form-item>
      <el-form-item label="IP地址" prop="ip">
        <el-input v-model="form.ip" v-if="false"></el-input>
        <div class="ipdiv">
          <el-input v-model="form.ip1" @input="checkInputIp1" @blur.native.capture="callGetHardwareInfo1"></el-input>
          <div class="dot">.</div>
          <el-input v-model="form.ip2" @input="checkInputIp2" @blur.native.capture="callGetHardwareInfo2"></el-input>
          <div class="dot">.</div>
          <el-input v-model="form.ip3" @input="checkInputIp3" @blur.native.capture="callGetHardwareInfo3"></el-input>
          <div class="dot">.</div>
          <el-input v-model="form.ip4" @input="checkInputIp4" @blur.native.capture="callGetHardwareInfo4"></el-input>
        </div>

      </el-form-item>
      <el-form-item label="MAC地址" prop="mac">
        <el-input v-model="form.mac" disabled></el-input>
      </el-form-item>
      <el-form-item label="主板序列号" prop="bios">
        <el-input v-model="form.bios" disabled></el-input>
      </el-form-item>
      <el-form-item label="输入注册码" prop="registercode">
        <el-input type="textarea" v-model="form.registercode" placeholder="请在此处输入技术供应商提供的注册码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click.native.prevent="handleauthLogin" class="login-authsubmit">{{$t('login.authsubmit')}}
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getHardwareInfo, softwareRegister } from '@/api/screen/register';
export default {
  name: 'verificationresult',
  data() {
    return {
      form: {
        companyName: '',
        ip: '',
        mac: '',
        bios: '',
        ip1: '',
        ip2: '',
        ip3: '',
        ip4: '',
        registercode: '',
      },
      ip: '',
    };
  },
  computed: {
    dataRule() {
      var validateContent = (rule, value, callback) => {
        callback();
      };
      return {
        companyName: [{ required: true, message: this.$t('validate.required'), trigger: 'blur' }],
        // mac: [
        //   {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        // ],
        // bios: [
        //   {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        // ],
        registercode: [{ required: true, message: this.$t('validate.required'), trigger: 'blur' }],
        ip: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' },
          { validator: validateContent, trigger: 'blur' },
        ],
      };
    },
  },
  methods: {
    handleauthLogin() {
      this.$refs['form'].validate((valid) => {
        if (!valid) {
          return false;
        }
        var params = {
          companyName: this.form.companyName,
          // mac: this.form.mac,
          // mainboardSN: this.form.bios,
          license: this.form.registercode,
          ip: this.form.ip,
        };
        softwareRegister(params)
          .then((res) => {
            this.$router.push({ path: '/login/authoresult', query: { success: true } });
          })
          .catch(() => {
            this.$router.push({ path: '/login/authoresult', query: { success: false } });
          });
      });
    },
    callGetHardwareInfo1() {
      if (this.form.ip1 && this.form.ip1 == 0) {
        this.$message.error(this.form.ip1 + '不是有效项。请指定一个介于1和223之间的值。');
        this.form.ip1 = 1;
      }
      if (this.form.ip1 > 223) {
        this.$message.error(this.form.ip1 + '不是有效项。请指定一个介于1和223之间的值。');
        this.form.ip1 = 223;
      }
      if (this.form.ip1 && this.form.ip2 && this.form.ip3 && this.form.ip4) {
        this.getHardwareInfo();
      }
    },
    callGetHardwareInfo2() {
      if (this.form.ip2 > 255) {
        this.$message.error(this.form.ip2 + '不是有效项。请指定一个介于0和255之间的值。');
        this.form.ip2 = 255;
      }
      if (this.form.ip1 && this.form.ip2 && this.form.ip3 && this.form.ip4) {
        this.getHardwareInfo();
      }
    },
    callGetHardwareInfo3() {
      if (this.form.ip3 > 255) {
        this.$message.error(this.form.ip3 + '不是有效项。请指定一个介于0和255之间的值。');
        this.form.ip3 = 255;
      }
      if (this.form.ip1 && this.form.ip2 && this.form.ip3 && this.form.ip4) {
        this.getHardwareInfo();
      }
    },
    callGetHardwareInfo4() {
      if (this.form.ip4 > 255) {
        this.$message.warning(this.form.ip4 + '不是有效项。请指定一个介于0和255之间的值。');
        this.form.ip4 = 255;
      }
      if (this.form.ip1 && this.form.ip2 && this.form.ip3 && this.form.ip4) {
        this.getHardwareInfo();
      }
    },
    getHardwareInfo() {
      var params = { ip: this.form.ip1 + '.' + this.form.ip2 + '.' + this.form.ip3 + '.' + this.form.ip4 };
      this.form.ip = params.ip;
      getHardwareInfo(params)
        .then((res) => {
          var returnData = res.data;
          if (returnData.success) {
            this.form.mac = returnData.data.mac;
            this.form.bios = returnData.data.mainboardSN;
          } else {
            thi.$message.error(returnData.msg);
            this.form.bios = null;
          }
        })
        .catch(() => {});
    },
    checkInputIp1() {
      this.form.ip1 = this.dealInputVal(this.form.ip1);
    },
    checkInputIp2() {
      this.form.ip2 = this.dealInputVal(this.form.ip2);
    },
    checkInputIp3() {
      this.form.ip3 = this.dealInputVal(this.form.ip3);
    },
    checkInputIp4() {
      this.form.ip4 = this.dealInputVal(this.form.ip4);
    },
    dealInputVal(value) {
      value = value.replace(/[^\d]/g, ''); //清除"数字"以外的字符
      value = value.replace(/^0*(0\.|[1-9])/, '$1');
      value = value.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.');
      value = value.indexOf('0') == 0 && value.length == 2 ? value.substring(1, 2) : value;
      value = value.length == 4 ? value.substring(0, 3) : value;
      return value;
    },
  },
};
</script>
<style lang="scss">
.authForm {
  .el-form-item__label {
    padding: 0;
  }
  .el-form-item__content {
    margin-bottom: 10px;
  }
  .ipdiv {
    display: flex;
    .dot {
      color: #000;
      font-weight: bolder;
    }
  }
}
</style>
